/**
 *
 * The MIT License
 *
 * Copyright (c) 2009 Noritaka Horio All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * @author Noritaka Horio 
 * @copyright Copyright (c) 2009 Noritaka Horio All rights reserved.
 * @license The MIT License
 * @version $Rev: 191 $ $Date: 2009-11-08 04:15:57 +0000 (Sun, 08 Nov 2009) $
 * @see http://mootools.net
 * 
 */

var TimePicker = {

	ampmValues: ["AM","PM"],
	timesValues: ["1","2","3","4","5","6","7","8","9","10","11","12"],
	minutesValues: ["00","10","20","30","40","50"],

	initialize: function() {
		this.container = $("timePicker");
		this.ampmElement  = $("ampm");
		this.hourElement  = $("hour");
		this.minutElement = $("minutes");
		this.ok = $(this.container).getElement("a.ok");
		this.cancel = $(this.container).getElement("a.cancel");
		this.title = $(this.container).getElement("span.title");
		this.initializeSelecter();
		this.bindEvents();
	},

	initializeSelecter: function() {
		this.ampmValues.each(function(v,k){
			var a  = new Element("a", {"html": v, "href": "#" + v});
			var li = new Element("li");
			a.inject(li);
			li.inject(this.ampmElement);
		}.bind(this));

		this.timesValues.each(function(v,k){
			var a  = new Element("a", {"html": v, "href": "#" + v});
			var li = new Element("li");
			a.inject(li);
			li.inject(this.hourElement);
		}.bind(this));

		this.minutesValues.each(function(v,k){
			var a  = new Element("a", {"html": v, "href": "#" + v});
			var li = new Element("li");
			a.inject(li);
			li.inject(this.minutElement);
		}.bind(this));
		this.ampms	 = $(this.ampmElement).getElements("a");
		this.hours	 = $(this.hourElement).getElements("a");
		this.minutes = $(this.minutElement).getElements("a");
	},

	bindEvents: function() {
		var buttons = this.ampms;
		buttons.each(function(element,k){
			element.addEvent("click", this.onClickAMPM.bind(this, [element,k]));
		}.bind(this));

		var buttons = this.hours;
		buttons.each(function(element,k){
			element.addEvent("click", this.onClickTimes.bind(this, [element,k]));
		}.bind(this));

		var buttons = this.minutes;
		buttons.each(function(element,k){
			element.addEvent("click", this.onClickMinutes.bind(this, [element,k]));
		}.bind(this));

		this.ok.addEvent("click", this.onClickOK.bind(this));
		this.cancel.addEvent("click", this.onClickCancel.bind(this));
	},

	onClickOK: function() {
		nativeWindow.close();
	},
	
	onClickCancel: function() {
		nativeWindow.close();
	},

	onClickAMPM: function(element,index) {
		var buttons = this.ampms;
		buttons.each(function(v,k){
			v.removeClass("selected");
		});
		element.addClass("selected");
		var value = this.getTime();
		this.setTime(value);
	},

	onClickTimes: function(element,index){
		var buttons = this.hours;
		buttons.each(function(v,k){
			v.removeClass("selected");
		});
		element.addClass("selected");
		var value = this.getTime();
		this.setTime(value);
	},

	onClickMinutes: function(element,index){
		var buttons = this.minutes;
		buttons.each(function(v,k){
			v.removeClass("selected");
		});
		element.addClass("selected");
		var value = this.getTime();
		this.setTime(value);
	},

	setTime: function(time) {
		var t = this.ampmValues.indexOf(time.ampm);
		var h = this.timesValues.indexOf(time.hour.toInt().toString());
		var m = this.minutesValues.indexOf(time.minut);
		this.ampms[t].addClass("selected");
		this.hours[h].addClass("selected");
		this.minutes[m].addClass("selected");

		t = this.ampms[t], h = this.hours[h], m = this.minutes[m];

		var title	= h.get("html") + " : " + m.get("html") + " " + t.get("html");
		this.title.set("html", title);
	},

	getTime: function() {
		var ampm	= $(this.ampmElement).getElement("a.selected");
		var hour	= $(this.hourElement).getElement("a.selected");
		var minut = $(this.minutElement).getElement("a.selected");
		var hv = hour.get("html"), mv = minut.get("html"), av = ampm.get("html");
		var time24 = "";

		if (av == "AM") {
			time24 = hv + ":" + mv;
		} else {
			time24 = (hv.toInt() + 12 >= 24) ? "00" : hv.toInt() + 12;
			time24 = time24 + ":" + mv;
		}

		var time = {
			"hour": hv,
			"minut": mv,
			"ampm": av,
			"h24": time24
		};
		return time;
	}
}

window.addEvent("domready", TimePicker.initialize.bind(TimePicker));